package com.demo.webapp.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.util.Assert;
import org.springframework.web.filter.GenericFilterBean;

/**
 * 安全上下文过滤器
 * 
 * @author ryuu.kk  at 2013-04-08 新增
 *
 */
public class SecurityContextHolderAwareRequestFilter extends GenericFilterBean {

	private String rolePrefix;

	public void setRolePrefix(String rolePrefix) {
		Assert.notNull(rolePrefix, "Role prefix must not be null");
		this.rolePrefix = rolePrefix.trim();
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		chain.doFilter(new SecurityContextHolderAwareRequestWrapper((HttpServletRequest) request, rolePrefix), response);
	}

}
